package com.alipay.api.internal.util;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayResponse;
import com.alipay.api.internal.parser.json.ObjectJsonParser;
import com.alipay.api.internal.util.json.JSONValidatingReader;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

public abstract class AlipayUtils {

   private static String localIp;


   public static String getFileSuffix(byte[] bytes) {
      return bytes != null && bytes.length >= 10?(bytes[0] == 71 && bytes[1] == 73 && bytes[2] == 70?"GIF":(bytes[1] == 80 && bytes[2] == 78 && bytes[3] == 71?"PNG":(bytes[6] == 74 && bytes[7] == 70 && bytes[8] == 73 && bytes[9] == 70?"JPG":(bytes[0] == 66 && bytes[1] == 77?"BMP":null)))):null;
   }

   public static String getMimeType(byte[] bytes) {
      String suffix = getFileSuffix(bytes);
      String mimeType;
      if("JPG".equals(suffix)) {
         mimeType = "image/jpeg";
      } else if("GIF".equals(suffix)) {
         mimeType = "image/gif";
      } else if("PNG".equals(suffix)) {
         mimeType = "image/png";
      } else if("BMP".equals(suffix)) {
         mimeType = "image/bmp";
      } else {
         mimeType = "application/octet-stream";
      }

      return mimeType;
   }

   public static Map cleanupMap(Map map) {
      if(map != null && !map.isEmpty()) {
         HashMap result = new HashMap(map.size());
         Set entries = map.entrySet();
         Iterator i$ = entries.iterator();

         while(i$.hasNext()) {
            Entry entry = (Entry)i$.next();
            if(entry.getValue() != null) {
               result.put(entry.getKey(), entry.getValue());
            }
         }

         return result;
      } else {
         return null;
      }
   }

   public static Map parseJson(String body) {
      JSONValidatingReader jr = new JSONValidatingReader();
      Object obj = jr.read(body);
      return obj instanceof Map?(Map)obj:null;
   }

   public static AlipayResponse parseResponse(String json, Class clazz) throws AlipayApiException {
      ObjectJsonParser parser = new ObjectJsonParser(clazz);
      return parser.parse(json);
   }

   public static String getLocalNetWorkIp() {
      if(localIp != null) {
         return localIp;
      } else {
         try {
            Enumeration e = NetworkInterface.getNetworkInterfaces();
            InetAddress ip = null;

            while(e.hasMoreElements()) {
               NetworkInterface ni = (NetworkInterface)e.nextElement();
               if(!ni.isLoopback() && !ni.isVirtual()) {
                  Enumeration addresss = ni.getInetAddresses();

                  while(addresss.hasMoreElements()) {
                     InetAddress address = (InetAddress)addresss.nextElement();
                     if(address instanceof Inet4Address) {
                        ip = address;
                        break;
                     }
                  }

                  if(ip != null) {
                     break;
                  }
               }
            }

            if(ip != null) {
               localIp = ip.getHostAddress();
            } else {
               localIp = "127.0.0.1";
            }
         } catch (Exception var5) {
            localIp = "127.0.0.1";
         }

         return localIp;
      }
   }
}
